class ParseXart
    
  require 'rubygems'
  require 'yaml'
  
  require 'nokogiri'
  require 'open-uri'
  require 'net/http'
  require 'mysql'
  
  @config = YAML::load(File.open("../config/config.yml")) 
  @dbconfig = YAML::load(File.open("../config/database.yml")) 
  @dbprod = @dbconfig['production']
  
  doc = Nokogiri::HTML(open(@config['x-art-affiliate-url']))
  
  divarry = doc.xpath("//html//body//div[@class='entrydata']")
  
  dbh = Mysql.real_connect(@dbprod['host'], @dbprod['username'], @dbprod['password'], @dbprod['database'])
  
  divarry.each  do |div|
    
    pel = div.xpath("p")
    nel = div.xpath("h4")
    ael = div.xpath("ul/li/a")
    
    name = nel[0].content
    desc = pel[1].content
    zip = ael[1].content
    added = pel[0].content
    
    query = "SELECT name FROM gallery WHERE name = '"+name+"'"
    res = dbh.query(query)
    if res.num_rows > 0
      next
    end
    
    puts name
    
    url = URI.parse(@config['create-gallery-url'])
    post_args1 = { 'auth' => @config['auth'], 'gallery[name]' => name, 'gallery[desc]' => desc, 'zip' => zip, 'added' => added, 'gallery[site_id]' => '1', 'gallery[cat_id]' => '1' }
    resp = Net::HTTP.post_form(url, post_args1)
    
  end
  
  dbh.close
 
end